package futbol.tacticas.concursantes.qualitycode.common;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.List;

import futbol.General;
import futbol.tacticas.concursantes.qualitycode.QCodeTactica;

public class QPlayer
{   
  protected final Point destPos   = new Point(QCodeTactica.INVALID, QCodeTactica.INVALID);
  
  protected final int id;
  protected double x = QCodeTactica.INVALID;
  protected double y = QCodeTactica.INVALID;  
  
  protected int force = QCodeTactica.FORCE_7_MAX;
  
/**
 * Constructor de copia 
 */
  public QPlayer(QPlayer player)
  {
    this.id = player.id;        
    this.destPos.setLocation(player.destPos);
    this.x = player.x;
    this.y = player.y;
    this.force = player.force;
  }
  
  public int speed()
  {
    return 10 - force;
  }

  public QPlayer(int id)
  {
    this.id = id;
  }
  
  public int getID()
  {
    return id;
  }
  
  public Point getPos()
  {
    return new Point((int)x,(int)y);
  }

  public double distance(QPlayer player)
  {
    return General.getDistancia(player.x, player.y, x, y);
  }
  
  public void reset()
  {}

  public QPlayer futurePos(Point dest, int iteraciones)
  {
    final QPlayer futurePlayer = new QPlayer(this);
    futurePlayer.setDestPos(dest);
    for (int i = 0; i < iteraciones; i++)
      futurePlayer.move();
    
    return futurePlayer;
  }

  public QBall interseptarBola(List<QBall> balls)
  {
    QBall minBall = balls.get(0);
    if (minBall.canGolpear(this))
      return minBall;
    
    double minDistancia = QUtils.distance(getPos(), minBall.getPos());    
    int i = 1;
    /*for(i = 1; i < balls.size(); i++)
      if (balls.get(i).isKickAble())
        break;/**/
    
    for(int j=1; i < balls.size(); i++,j++)
    {      
      final QBall ball = balls.get(i);
      final QPlayer futurePlayer = futurePos(ball.getPos(), j);
      if (ball.canGolpear(futurePlayer))
      {
        final double minDistancia2 = QUtils.distance(futurePlayer.getPos(), ball.getPos());
        if (minDistancia > minDistancia2)
        {
          minDistancia = minDistancia2;
          minBall = ball;
        }
      }
    }
    
    if (balls.size() > 1 && minBall == balls.get(0))
      return balls.get(balls.size()-1);
    
    return minBall; 
  }

  public int turnosInterseptarBola(List<QBall> balls)
  {    
    if (balls.get(0).canGolpear(this))
      return 0;
    
    int kickAble = 1;
    /*for(kickAble = 1; kickAble < balls.size(); kickAble++)
      if (balls.get(kickAble).isKickAble())
        break;/**/
    
    for(int j=1; kickAble < balls.size(); kickAble++,j++)
    {      
      final QBall ball = balls.get(kickAble);
      final QPlayer futurePlayer = futurePos(ball.getPos(), j);
      if (ball.canGolpear(futurePlayer))
        return kickAble;
    }
    
    final QBall lastBall = balls.get(balls.size()-1);
    int turnos = balls.size();
    final QPlayer futurePlayer = futurePos(lastBall.getPos(), balls.size());
    for (;!futurePlayer.canGolpear(lastBall) && turnos < 1000; turnos++)
      futurePlayer.move();
    
    return turnos; 
  }

//-----------------------------------------------------------------------
  
  public boolean canGolpear(QBall ball)
  {
    return ball.canGolpear(this);
  }
  
  public void setDestPos(Point dest)
  {
    this.destPos.setLocation(dest);
  }
  
  public int getForce()
  {
    return force;
  }

  public void updatePos(Point point)
  {    
    x = point.x;
    y = point.y;    
  }

  public void move()
  {
    if (General.getDistancia(x, y, destPos.x, destPos.y) > speed())
    {
      final double angulo = General.getAngulo(x, y, destPos.x, destPos.y);
      x += Math.cos(angulo) * speed();
      y += Math.sin(angulo) * speed();
    }
    else
    {
      x = destPos.x; 
      y = destPos.y;
    }
  }

//-----------------------------------------------------------------------
  
  public String toString()
  {
    return String.format("Player(%d) - F(%d) pos(%f,%f) ", id+1, force, x, y);
  }

  public int getX()
  {
    return (int) x;
  }
  
  public int getY()
  {
    return (int) y;
  }

  public void pintar(boolean over, Graphics gdb)
  {
    if (over)
    {
      gdb.setColor(Color.WHITE);
      gdb.drawString("  "+(int)x+" "+(int)y, 141+160-(int)x, 91+120-(int)y);
    }
  }
    
}






























